﻿package mortal.game.scene3D.map3D
{
    import baseEngine.core.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.util.*;

    public class MapVague extends Object
    {
        private var pieceWidth:Number = 64;
        private var pieceHeight:Number = 64;
        private var rateX:Number;
        private var rateY:Number;
        private var _gridMap:Dictionary;
        private var _minMapbmpdata:BitmapData;
        private var _poolList:Array;
        private var _curLoaderInfo:ImageInfo;
        private var _hasLoaedMapData:Boolean = false;
        private static const _matrix:Matrix = new Matrix();
        public static var instance:MapVague = new MapVague;

        public function MapVague()
        {
            this._poolList = [];
            if (instance)
            {
                throw new Error("单例！");
            }
            return;
        }// end function

        public function dispose() : void
        {
            var _loc_2:Texture3D = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            this._hasLoaedMapData = false;
            if (this._minMapbmpdata)
            {
                this._minMapbmpdata.dispose();
                this._minMapbmpdata = null;
            }
            var _loc_1:Array = [];
            for each (_loc_2 in this._gridMap)
            {
                
                _loc_1.push(_loc_2);
            }
            _loc_3 = _loc_1.length;
            _loc_4 = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_2 = _loc_1[_loc_4];
                _loc_2.disposeImp();
                this._poolList.push(_loc_2);
                _loc_4++;
            }
            this._gridMap = new Dictionary(false);
            if (this._curLoaderInfo != null)
            {
                LoaderManager.instance.removeResourceEvent(this._curLoaderInfo.name, this.loadHander);
                this._curLoaderInfo = null;
            }
            return;
        }// end function

        public function loadMinMap() : void
        {
            this._curLoaderInfo = SceneConfig.instance.getImageInfo(MapFileUtil.mapScene);
            LoaderManager.instance.load(this._curLoaderInfo.name, this.loadHander, 0);
            return;
        }// end function

        public function set hasLoaedMapData(param1:Boolean) : void
        {
            this._hasLoaedMapData = param1;
            this.loadHander(null);
            return;
        }// end function

        private function loadHander(param1:ImageInfo) : void
        {
            var rateMapX:Number;
            var rateMapY:Number;
            var ratePieceX:Number;
            var ratePieceY:Number;
            var infor:* = param1;
            if (!this._hasLoaedMapData || !this._curLoaderInfo || !this._curLoaderInfo.bitmapData || this._curLoaderInfo.isDispose)
            {
                return;
            }
            var old_minMapbmpdata:* = this._curLoaderInfo.bitmapData;
            var numX:* = Math.ceil(GameMapUtil.mapWidth / MapConst.pieceWidth);
            var numY:* = Math.ceil(GameMapUtil.mapHeight / MapConst.pieceHeight);
            var avaliable:* = numX > 0 && numX < 1000 && (numY > 0 && numY < 1000);
            if (!avaliable)
            {
                return;
            }
            try
            {
                rateMapX = GameMapUtil.mapWidth / old_minMapbmpdata.width;
                rateMapY = GameMapUtil.mapHeight / old_minMapbmpdata.height;
                ratePieceX = MapConst.pieceWidth / this.pieceWidth;
                ratePieceY = MapConst.pieceHeight / this.pieceHeight;
                this.rateX = rateMapX / ratePieceX;
                this.rateY = rateMapY / ratePieceY;
                if (this._minMapbmpdata)
                {
                    this._minMapbmpdata.dispose();
                }
                this._minMapbmpdata = new BitmapData(numX * this.pieceWidth, numY * this.pieceHeight);
                _matrix.identity();
                _matrix.scale(this.rateX, this.rateY);
                this._minMapbmpdata.draw(old_minMapbmpdata, _matrix, null, null, null, true);
            }
            catch (e:Error)
            {
                if (_minMapbmpdata)
                {
                    _minMapbmpdata = null;
                }
            }
            return;
        }// end function

        public function loadPiece(param1:ImageInfo) : Texture3D
        {
            var _loc_8:Texture3D = null;
            if (!this._minMapbmpdata)
            {
                return null;
            }
            if (!param1 || !param1.extData)
            {
                return null;
            }
            var _loc_2:* = param1.extData.x;
            var _loc_3:* = param1.extData.y;
            var _loc_4:* = "" + _loc_2 + ":" + _loc_3;
            if (this._gridMap[_loc_4])
            {
                return this._gridMap[_loc_4];
            }
            var _loc_5:* = _loc_2 * this.pieceWidth;
            var _loc_6:* = _loc_3 * this.pieceHeight;
            var _loc_7:* = new BitmapData(this.pieceWidth, this.pieceHeight);
            _matrix.identity();
            _matrix.translate(-_loc_5, -_loc_6);
            _loc_7.draw(this._minMapbmpdata, _matrix, null, null, null, true);
            if (this._poolList.length > 0)
            {
                _loc_8 = this._poolList.shift();
                _loc_8.request = _loc_7;
            }
            else
            {
                _loc_8 = new Texture3D(_loc_7, 0);
            }
            _loc_8.immediatelyUpload();
            _loc_8.clearSourceData();
            this._gridMap[_loc_4] = _loc_8;
            return _loc_8;
        }// end function

    }
}
